package org.microsites.domain.dao.eod;

import java.util.List;

import net.lemnik.eodsql.BaseQuery;
import net.lemnik.eodsql.DataSet;
import net.lemnik.eodsql.GeneratedKeys;
import net.lemnik.eodsql.Select;
import net.lemnik.eodsql.Update;

import org.microsites.domain.dao.ProjectDAO;
import org.microsites.domain.model.Project;
import org.microsites.util.EodDaoSupport;

public class ProjectDAOImpl extends EodDaoSupport implements ProjectDAO {
	private Query query;
	
	public Query getQuery() {
		return query == null ? query = super.getQuery(Query.class) : query;
	}

	@Override
	public void create(Project p) {
		p.setId(getQuery().create(p));
		for (Long fileset : p.getFileSet()) {
			getQuery().createFileSet(p.getId(), fileset);
		}
	}

	@Override
	public void delete(Long id) {
		getQuery().delete(id);
		getQuery().deleteFileSet(id);
	}

	@Override
	public List<Project> list() {
		return initFileSet(getQuery().list());
	}

	@Override
	public List<Project> sublist(Long id) {
		return initFileSet(getQuery().sublist(id));
	}
	
	@Override
	public List<Project> sublistInGroup(Long id) {
		return initFileSet(getQuery().sublistInGroup(id));
	}
	
	protected List<Project> initFileSet(List<Project> list) {
		for (Project p : list) {
			p.setFileSet(getQuery().loadFileSet(p.getId()));
		}
		return list;
	}

	@Override
	public Project load(Long id) {
		Project p = getQuery().load(id);
		p.setFileSet(getQuery().loadFileSet(id));
		return p;
	}

	@Override
	public void update(Project p) {
		this.query.deleteFileSet(p.getId());
		getQuery().update(p);
		for (Long fileset : p.getFileSet()) {
			getQuery().createFileSet(p.getId(), fileset);
		}
	}

	@Override
	public List<String> autocomplete(String column, String pattern) {
		return super.select(String.class, "SELECT " + column + " FROM Project WHERE " + column + " LIKE ?1 GROUP BY " + column, '%' + pattern + '%');
	}

	static interface Query extends BaseQuery {
		
		@Select(sql = "SELECT * FROM Project")
		public List<Project> list();
		
		@Select(sql = "SELECT * FROM Project WHERE [parent]=?1")
		public List<Project> sublist(Long id);
		
		@Select(sql = "SELECT * FROM Project WHERE [group]=?1")
		public List<Project> sublistInGroup(Long id);
		
		@Select(sql = "SELECT fileset FROM Project_FileSet ORDER BY project")
		public DataSet<Long> listFileSet();
		
		@Select(sql = "SELECT * FROM Project WHERE id=?1")
		public Project load(Long id);

		@Select(sql = "SELECT fileset FROM Project_FileSet WHERE project=?1")
		public List<Long> loadFileSet(Long id);

        @Update(sql = "INSERT INTO Project (name,description,[group],[parent]) VALUES (?{1.name},?{1.description},?{1.group},?{1.parent})",
                keys = GeneratedKeys.RETURNED_KEYS_FIRST_COLUMN)
		public Long create(Project p);
		
        @Update(sql = "INSERT INTO Project_FileSet (project,fileset) VALUES (?1,?2)")
		public void createFileSet(Long project, Long fileset);
		
        @Update(sql = "UPDATE Project SET name=?{1.name},description=?{1.description} WHERE id=?{1.id}")
		public void update(Project p);
		
		@Update(sql = "DELETE FROM Project WHERE id=?1")
		public void delete(Long id);
		
		@Update(sql = "DELETE FROM Project_FileSet WHERE project=?1")
		public void deleteFileSet(Long project);
	}
}
